﻿CREATE TABLE [CD].[ManageDocLines] (
    [Id]                   INT             NOT NULL,
    [DepartmentId]         AS              ([Id]/(100000000)),
    [DocId]                INT             NOT NULL,
    [GoodsVsContragentsId] INT             NULL,
    [GoodsId]              INT             CONSTRAINT [DF_ManageDocLines_GoodsId] DEFAULT ((1)) NOT NULL,
    [Qntty]                INT             CONSTRAINT [DF_ManageDocLines_Qntty] DEFAULT ((1)) NULL,
    [QnttySum]             DECIMAL (19, 2) CONSTRAINT [DF_ManageDocLines_QnttySum] DEFAULT ((1)) NULL,
    [Note]                 VARCHAR (250)   NULL,
    [MD]                   DATETIME        CONSTRAINT [DF__ManageDocLin__MD__675F4696] DEFAULT (getdate()) NOT NULL,
    [MW]                   VARCHAR (50)    CONSTRAINT [DF__ManageDocLin__MW__68536ACF] DEFAULT (suser_sname()) NOT NULL,
    CONSTRAINT [PK_ManageDocLines] PRIMARY KEY NONCLUSTERED ([Id] ASC),
    CONSTRAINT [FK_ManageDocLines_Documents] FOREIGN KEY ([DocId]) REFERENCES [CA].[Documents] ([Id]) NOT FOR REPLICATION,
    CONSTRAINT [FK_ManageDocLines_Goods] FOREIGN KEY ([GoodsId]) REFERENCES [CA].[Goods] ([Id]) NOT FOR REPLICATION,
    CONSTRAINT [FK_ManageDocLines_GoodsVsContragents] FOREIGN KEY ([GoodsVsContragentsId]) REFERENCES [CA].[GoodsVsContragents] ([Id]) NOT FOR REPLICATION
);


GO
CREATE NONCLUSTERED INDEX [IX_ManageDocLines_DepartmentId]
    ON [CD].[ManageDocLines]([DepartmentId] ASC);


GO
CREATE NONCLUSTERED INDEX [IX_ManageDocLines_MD]
    ON [CD].[ManageDocLines]([MD] ASC);


GO
CREATE NONCLUSTERED INDEX [IX_ManageDocLines_DocId]
    ON [CD].[ManageDocLines]([DocId] ASC);


GO

CREATE TRIGGER CD.ManageDocLinesDeleteLog ON CD.ManageDocLines AFTER DELETE
AS 
BEGIN
	SET NOCOUNT ON;

INSERT INTO [PR].[DocDeleteLog] (TableName, IdValue, MD, MW)
SELECT 'CD.ManageDocLines', Id, getdate(), SYSTEM_USER FROM DELETED

END
